#!/usr/bin/env python
# -*- coding: utf-8 -*-

from flask import jsonify
from typing import Any, Dict, List, Optional, Union


def api_response(data=None, message="操作成功", status_code=200):
    """
    标准API成功响应格式
    
    Args:
        data: 响应数据
        message: 响应消息
        status_code: HTTP状态码
    
    Returns:
        标准格式的JSON响应
    """
    response = {
        "success": True,
        "message": message,
        "data": data
    }
    return jsonify(response), status_code


def api_success(data=None, message="操作成功", status=200):
    """
    成功响应
    
    Args:
        data: 响应数据
        message: 响应消息
        status: HTTP状态码
    
    Returns:
        JSON响应
    """
    return api_response(data, message, status)


def api_error_response(message="操作失败", status_code=400, errors=None):
    """
    标准API错误响应格式
    
    Args:
        message: 错误消息
        status_code: HTTP状态码
        errors: 详细错误信息(可选)
    
    Returns:
        标准格式的JSON错误响应
    """
    response = {
        "success": False,
        "message": message,
    }
    
    if errors:
        response["errors"] = errors
        
    return jsonify(response), status_code


class APIResponse:
    """统一API响应工具类"""

    @staticmethod
    def pagination(
        items: List[Any], 
        total: int, 
        page: int, 
        per_page: int, 
        message: str = "success", 
        code: int = 0
    ) -> Dict:
        """
        分页响应
        
        Args:
            items: 当前页数据
            total: 总数据数
            page: 当前页码
            per_page: 每页数量
            message: 响应消息
            code: 响应码
            
        Returns:
            标准化的分页响应JSON
        """
        return jsonify({
            'code': code,
            'message': message,
            'data': {
                'items': items,
                'total': total,
                'page': page,
                'per_page': per_page,
                'pages': (total + per_page - 1) // per_page
            }
        }) 